package com.jiankangjin.appspos.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.appspos.services.UserServices;
import com.jiankangjin.appspos.vo.CertiCodeResendVo;
import com.jiankangjin.appspos.vo.GetUserInfoVo;
import com.jiankangjin.appspos.vo.UpdPasswordVo;
import com.jiankangjin.appspos.vo.UserLoginVo;
import com.jiankangjin.appspos.vo.UserLogoutVo;
import com.jiankangjin.appspos.vo.ValidateUserCertiVo;

/**  
 * 用户登录<br>
 * 第一步：获取图片验证码<br>
 * 第二步：帐号，密码，验证码登陆<br>
 * 第三步：发送短信码<br>
 * 第四步：验证短信码 <br>
 */
@Controller
public class UserController {
	private static final transient Log logger = LogFactory.getLog(UserController.class);
	
	@Autowired
	private UserServices userService;

	/**
	 * 用户登陆
	 */
	@RequestMapping(value="/user_login", method=RequestMethod.POST)
	@ResponseBody
	public UserLoginVo userLogin(
			@RequestParam(required = false) String tmp_ssn,
			@RequestParam(required = false) String validate_code,
			@RequestParam(required = false) String login_voucher,
			@RequestParam(required = false) String login_password,
			@RequestParam(required = false) String terminal_type,
			@RequestParam(required = false) String terminal_ver,
			@RequestParam(required = false) String terminal_id,
			@RequestParam(required = false) String sign){
		StringBuilder sb = new StringBuilder();
		sb.append("tmp_ssn=" + tmp_ssn);
		sb.append(" validate_code=" + validate_code);
		sb.append(" login_voucher=" + login_voucher);
		sb.append(" login_password=" + login_password);
		sb.append(" terminal_type=" + terminal_type);
		sb.append(" terminal_ver=" + terminal_ver);
		sb.append(" terminal_id=" + terminal_id);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("用户登陆接口:" + sb);
		}
		UserLoginVo userLoginVo = new UserLoginVo();
		userLoginVo.setResp_status("9999");
		if (tmp_ssn == null || tmp_ssn.equals("")) {
			userLoginVo.setMsg("tmp_ssn不能为空");
			return userLoginVo;
		}
		if (validate_code == null || validate_code.equals("")) {
			userLoginVo.setMsg("validate_code不能为空");
			return userLoginVo;
		}
		if (login_voucher == null || login_voucher.equals("")) {
			userLoginVo.setMsg("login_voucher不能为空");
			return userLoginVo;
		}
		if (login_password == null || login_password.equals("")) {
			userLoginVo.setMsg("login_password不能为空");
			return userLoginVo;
		}
		if (terminal_type == null || terminal_type.equals("")) {
			userLoginVo.setMsg("terminal_type不能为空");
			return userLoginVo;
		}
		if (terminal_ver == null || terminal_ver.equals("")) {
			userLoginVo.setMsg("terminal_ver不能为空");
			return userLoginVo;
		}
		if (terminal_id == null || terminal_id.equals("")) {
			userLoginVo.setMsg("terminal_id不能为空");
			return userLoginVo;
		}
		if (sign == null || sign.equals("")) {
			userLoginVo.setMsg("sign不能为空");
			return userLoginVo;
		}
		return userService.userLogin(tmp_ssn, validate_code, login_voucher, login_password, terminal_type, terminal_ver, terminal_id);
	}
	
	/**
	 * 身份校验凭证重发
	 */
	@RequestMapping(value="/certi_code_resend", method=RequestMethod.POST)
	@ResponseBody
	public CertiCodeResendVo certiCodeResend(
			@RequestParam(required = false) String token_low,
			@RequestParam(required = false) String sign){
		StringBuilder sb = new StringBuilder();
		sb.append("token_low=" + token_low);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("身份校验凭证重发接口:" + sb);
		}
		CertiCodeResendVo certiCodeResendVo = new CertiCodeResendVo();
		certiCodeResendVo.setResp_status("9999");
		if (token_low == null || token_low.equals("")) {
			certiCodeResendVo.setMsg("token_low不能为空");
			return certiCodeResendVo;
		}
		if (sign == null || sign.equals("")) {
			certiCodeResendVo.setMsg("sign不能为空");
			return certiCodeResendVo;
		}
		return userService.certiCodeResend(token_low);
	}
	
	/**
	 * 身份校验(短信码)请求 
	 */
	@RequestMapping(value="/validate_user_certi", method=RequestMethod.POST)
	@ResponseBody
	public ValidateUserCertiVo validateUserCerti(
			@RequestParam(required = false) String token_low,
			@RequestParam(required = false) String certi_code,
			@RequestParam(required = false) String sign){
		StringBuilder sb = new StringBuilder();
		sb.append("token_low=" + token_low);
		sb.append(" certi_code=" + certi_code);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("身份校验(短信码)请求接口:" + sb);
		}
		ValidateUserCertiVo validateUserCertiVo = new ValidateUserCertiVo();
		validateUserCertiVo.setResp_status("9999");
		if (token_low == null || token_low.equals("")) {
			validateUserCertiVo.setMsg("token_low不能为空");
			return validateUserCertiVo;
		}
		if (certi_code == null || certi_code.equals("")) {
			validateUserCertiVo.setMsg("certi_code不能为空");
			return validateUserCertiVo;
		}
		if (sign == null || sign.equals("")) {
			validateUserCertiVo.setMsg("sign不能为空");
			return validateUserCertiVo;
		}
		return userService.validateUserCerti(token_low, certi_code);
	}
	
	/**
	 * 下载用户信息
	 */
	@RequestMapping(value="/get_user_info", method=RequestMethod.POST)
	@ResponseBody
	public GetUserInfoVo getUserInfo(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String sign){
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("下载用户信息接口:" + sb);
		}
		GetUserInfoVo getUserInfoVo = new GetUserInfoVo();
		getUserInfoVo.setResp_status("9999");
		if (token == null || token.equals("")) {
			getUserInfoVo.setMsg("token不能为空");
			return getUserInfoVo;
		}
		if (sign == null || sign.equals("")) {
			getUserInfoVo.setMsg("sign不能为空");
			return getUserInfoVo;
		}
		return userService.getUserInfo(token);
	}
	
	
	/**
	 * 密码更新
	 */
	@RequestMapping(value="/upd_password", method=RequestMethod.POST)
	@ResponseBody
	public UpdPasswordVo updPassword(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String old_password,
			@RequestParam(required = false) String new_password,
			@RequestParam(required = false) String sign){
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" old_password=" + old_password);
		sb.append(" new_password=" + new_password);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("密码更新接口:" + sb);
		}
		UpdPasswordVo updPasswordVo = new UpdPasswordVo();
		updPasswordVo.setResp_status("9999");
		if (token == null || token.equals("")) {
			updPasswordVo.setMsg("token不能为空");
			return updPasswordVo;
		}
		if (old_password == null || old_password.equals("")) {
			updPasswordVo.setMsg("old_password不能为空");
			return updPasswordVo;
		}
		if (new_password == null || new_password.equals("")) {
			updPasswordVo.setMsg("new_password不能为空");
			return updPasswordVo;
		}
		if (sign == null || sign.equals("")) {
			updPasswordVo.setMsg("sign不能为空");
			return updPasswordVo;
		}
		return userService.updPassword(token, old_password, new_password);
	}
	
	/**
	 * 登陆注销
	 */
	@RequestMapping(value="/user_logout", method=RequestMethod.POST)
	@ResponseBody
	public UserLogoutVo userLogout(
			@RequestParam(required = false) String token,
			@RequestParam(required=false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("登陆注销接口:" + sb);
		}
		UserLogoutVo userLogoutVo = new UserLogoutVo();
		userLogoutVo.setResp_status("9999");
		if (token == null || token.equals("")) {
			userLogoutVo.setMsg("token不能为空");
			return userLogoutVo;
		}
		if (sign == null || sign.equals("")) {
			userLogoutVo.setMsg("sign不能为空");
			return userLogoutVo;
		}
		return userService.userLogout(token);
	}
	
}
